/**
 * @description: 登录验证
 * @param {*}
 * @return {*}
 */
class Login {
    constructor() {
        this.username = $('input[name = "username"]')
        this.password = $('input[name = "password"]')
    }
    /* 获取账号密码数据并验证 */
    onLogin() {
        $('.login_button').on('click', () => {
            $.post('http://192.168.1.110:8088/api/login', {
                    username: this.username.val(),
                    password: this.password.val()
                },
                function (result) {
                    if (result.resultCode == 1) {
                        let user = result.resultInfo
                        localStorage.setItem('USER', JSON.stringify(user))
                        location.href = './index.html'
                    } else {
                        $('.tip').css('visibility', 'visible')
                    }
                })
        })
    }

    /* 注册用户 */
    onRegister() {
        $('.register_button').on('click', () => {
            let usernameValue = $('#login_account').val()
            let passwordValue = $('#login_password').val()
            let usernameReg = /\w{3,8}/g
            let passwordRge = /[0-9A-Za-z]{3,8}/g
            if(usernameValue == '' || usernameReg.test(usernameValue.trim()) == false || passwordValue == '' || passwordRge.test(passwordValue) == false){
                $('.container .main .tip').css('visibility','visible')
            }else{
                $.post('http://192.168.1.110:8088/api/register', {
                    username: usernameValue,
                    password: passwordValue
                },
                function (result) {
                    console.log(result)
                    if (result.resultCode == 1) {
                        alert('注册成功！')
                        $('.option li').toggleClass('dynamic')
                        $('.card li').toggleClass('active')
                    }
                })
            }
          
        })
    }
    /* 点击切换二维码登录 */
    onChange() {
        $(".header").find('img').on('click', () => {
            $('.login_content_a').toggleClass('active')
            $('.login_content_b').toggleClass('active')
        })
    }

    /* 点击切换注册和登录页面 */
    onCut() {
        $(".option li").on('click', () => {
            $('.option li').toggleClass('dynamic')
            $('.card li').toggleClass('active')
        })
    }

    /* 注册页面获取焦点，弹出提示 */
    onFocus() {
        $('.active .login_item').find('input').on('focus', (e) => {
            e = e || window.event
            let target = e.target || e.srcElement
            target.nextElementSibling.style.display = "block"
        })
        $('.active .login_item').find('input').on('blur', (e) => {
            e = e || window.event
            let target = e.target || e.srcElement
            target.nextElementSibling.style.display = "none"
        })
    }
}